package com.gress.shop.service.inner.impl;

import com.gress.shop.bo.Member;
import com.gress.shop.bo.example.MemberExample;
import com.gress.shop.dao.IMemberDao;
import com.gress.shop.service.inner.IMemberInternalService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MemberInternalService  implements IMemberInternalService{

    @Resource
    private IMemberDao memberDao;

    @Override
    public Member queryByEmail(String email) {
        MemberExample query = new MemberExample();
        query.createCriteria().andEmailEqualTo(email);
        List<Member> members = memberDao.queryMember(query);
        if (CollectionUtils.isEmpty(members)) {
            return null;
        }
        return members.get(0);
    }

    @Override
    public Member queryById(String id) {
        return memberDao.selectByPrimaryKey(id);
    }

    @Override
    public Member insertMember(Member member) {
        return memberDao.insertMember(member);
    }

    @Override
    public boolean updateMemberPSDByEmail(String email, String newPsd) {
        return memberDao.updateMemberPSDByEmail(email, newPsd);
    }


    @Override
    public String getMemberIdByEmail(String email) {
        Member member = queryByEmail(email);
        if (member == null) {
            return null;
        }else {
            return member.getId();
        }
    }

    @Override
    public boolean checkIsEmpty(String email) {
        return queryByEmail(email) == null;
    }
}
